import type { IFormItem } from '@/components/NFormBuilder/types'
import type {
  InputProps,
  InputNumberProps,
  SelectProps,
  CheckboxGroupProps,
  RadioGroupProps,
  DatePickerProps,
  SwitchProps,
  TreeSelectProps,
} from 'naive-ui'

export class ScConstants {
  span: number = 24
  constructor(span?: number) {
    if (span) this.span = span
  }
  /**
   * INPUT输入框
   * */
  public input(
    label: string,
    field: string,
    props: InputProps | Record<string, unknown> = {},
  ): IFormItem<InputProps> {
    const defaultProps: InputProps = {
      clearable: true,
    }
    return {
      label,
      field,
      span: this.span,
      props: {
        ...defaultProps,
        ...props,
      },
    }
  }

  /**
   * 下拉菜单
   * */
  public select(
    label: string,
    field: string,
    props: SelectProps | Record<string, unknown> = {},
  ): IFormItem<SelectProps> {
    const defaultProps: SelectProps = {
      clearable: true,
      filterable: true,
      labelField: 'label',
      valueField: 'value',
    }
    return {
      label,
      field,
      span: this.span,
      type: 'select',
      props: {
        ...defaultProps,
        ...props,
      },
    }
  }

  /**
   * Number输入框
   * */
  public number(
    label: string,
    field: string,
    props: InputNumberProps | Record<string, unknown> = {},
  ): IFormItem<InputNumberProps> {
    const defaultProps: InputNumberProps = {
      clearable: true,
      showButton: false,
    }
    return {
      label,
      field,
      span: this.span,
      type: 'number',
      props: {
        ...defaultProps,
        ...props,
      },
    }
  }

  /**
   * 开关
   * */
  public switch(label: string, field: string, props: SwitchProps | Record<string, unknown> = {}) {
    return {
      label,
      field,
      span: this.span,
      type: 'switch',
      props: props,
    }
  }

  /**
   * 单选框
   * */
  public radio(
    label: string,
    field: string,
    props: RadioGroupProps | Record<string, unknown> = {},
  ): IFormItem<RadioGroupProps> {
    return {
      label,
      field,
      span: 12,
      type: 'radio',
      props: {
        ...props,
      },
    }
  }

  /**
   * 复选框
   * */
  public checkbox(
    label: string,
    field: string,
    props: CheckboxGroupProps | Record<string, unknown> = {},
  ): IFormItem<CheckboxGroupProps> {
    return {
      label,
      field,
      span: 12,
      type: 'checkbox',
      props: {
        ...props,
      },
    }
  }

  /**
   * 格日期输入框
   * */
  public date(
    label: string,
    field: string | string[],
    props: DatePickerProps | Record<string, unknown> = {},
  ): IFormItem<DatePickerProps> {
    const defaultProps: DatePickerProps = {
      type: 'date',
      clearable: true,
    }
    return {
      label,
      field,
      span: this.span,
      type: 'datePicker',
      props: {
        ...defaultProps,
        ...props,
      },
    }
  }

  /**
   * 格式化日期输入框
   * */
  public dateFormat(
    label: string,
    field: string | string[],
    props: DatePickerProps | Record<string, unknown> = {},
  ): IFormItem<DatePickerProps> {
    const defaultProps: DatePickerProps = {
      type: 'date',
      clearable: true,
    }
    return {
      label,
      field,
      span: this.span,
      type: 'datePickerFormat',
      props: {
        ...defaultProps,
        ...props,
      },
    }
  }

  /**
   * 树形选择器
   * */
  public tree(
    label: string,
    field: string | string[],
    props: TreeSelectProps | Record<string, unknown> = {},
  ): IFormItem<TreeSelectProps> {
    const defaultProps: TreeSelectProps = {
      keyField: 'id',
      labelField: 'name',
      childrenField: 'children',
      clearable: true,
    }
    return {
      label,
      field,
      span: this.span,
      type: 'tree-select',
      props: {
        ...defaultProps,
        ...props,
      },
    }
  }

  /**
   * 图片选择
   * */
  public image(label: string, field: string, props: Record<string, unknown> = {}) {
    return {
      label,
      field,
      span: this.span,
      type: 'image',
      props: {
        ...props,
      },
    }
  }

  /**
   * 图片列表选择
   * */
  public imageList(label: string, field: string, props: Record<string, unknown> = {}) {
    return {
      label,
      field,
      span: this.span,
      type: 'imageList',
      props: {
        ...props,
      },
    }
  }

  /**
   * 附件选择
   * */
  public file(label: string, field: string, props: Record<string, unknown> = {}) {
    return {
      label,
      field,
      span: this.span,
      type: 'file',
      props: {
        ...props,
      },
    }
  }

  /**
   * 富文本
   * */
  public rich(label: string, field: string, props: Record<string, unknown> = {}) {
    return {
      label,
      field,
      span: this.span,
      type: 'rich',
      props: {
        ...props,
      },
    }
  }

  /**
   * 自定义组件
   * */
  public other(label: string, field: string, type: Component, props: Record<string, unknown> = {}) {
    return {
      label,
      field,
      type: markRaw(type),
      props,
    }
  }
}
